function list_admin_init() {
	getParentDep(); //大部门信息
	//管理员列表删除按钮
	$(".btn_remove").click(function() {
			var link = $(this);
			var act = $(this).attr("act");
			var msg = '';
			if(act == 'del'){
				msg = '确定删除管理员？';
			}
			else{
				msg = '确定恢复？';
			}
			$("#confirm_div_text").text(msg);
			$("#confirm_div").showMiddleDialog(["confirm_close", "confirm_ok", "confirm_cancel"], function() {
				if ($(this).attr("id") != "confirm_ok") {
					return;
				}

				var rel = link.attr("rel");
				$.post("/admin/del.php", "action="+act+"&staff=" + rel, function(json) {
					if (json.status == 1) {
						window.location.reload();
					} else {
						$("#confirm_div_text").text(json.msg);
						$("#confirm_div").showMiddleDialog(["confirm_close", "confirm_ok", "confirm_cancel"]);
					}
					}, "json");
				});
	});

	//添加管理员的全选，反选
	$(".sel_btn").click(function(){
				var rel = $(this).attr("rel");
				var type = $(this).attr("type");	//type=1 全选 type=2返选
				rel = "bigclass_"+rel;	//当前要选的id下的所有checkbox
				if(type == 1){
					$("#"+rel).find("input[name='power[]']").attr("checked", true);
				}
				else{
					$("#"+rel).find("input[name='power[]']").each(function(){
						$(this).attr("checked") == true ? $(this).attr('checked', false) : $(this).attr('checked', true);
						});
				}
			});

	//搜索
	$("#admin_search_type").click(function(){
		obj = $("#admin_search_value");
		if (obj.val() == '') {
//			alert("查询关键字不能为空！");
//			return false;
		}
		$("#admin_search_type_form").submit();
	});
	//部门搜索
	$("#admin_search_dep").click(function(){
		$("#admin_search_dep_form").submit();
	});
	//取staff的部门信息展示
	$("#showstaff").click(function(){
		$("#staffdetail").fadeOut();
		$("#staffphone").fadeOut();
		var staff  = $("input:[name='staff']").val();
		if(!$.trim(staff)){
			return false;
		}
		$.get("aj_staff.php?staff="+staff, "", function(rs){
			var _msg = eval("("+rs+")");
			if(!_msg.error){
				$("#staffphone").fadeIn();
				$("input:[name='phone']").val(_msg.phone);
				$("#staffdetail").html();
				tmp = "<font color=green>姓名    </font>："+_msg.name+"&nbsp;&nbsp;&nbsp;";
				tmp +="<font color=green>分机：</font>"+_msg.phone+"&nbsp;&nbsp;&nbsp;";
				tmp +="<font color=green>手机：</font>"+_msg.mobile+"&nbsp;&nbsp;&nbsp;";
				tmp +="<font color=green>部门：</font>"+_msg.subdep+"&nbsp;&nbsp;&nbsp;";
				tmp +="<font color=green>地区：</font>"+_msg.area+"&nbsp;&nbsp;&nbsp;";
				$("#staffdetail").html("<th>&nbsp;</th><td>"+tmp+"</td>");
				$("#staffdetail").fadeIn();
			}else{
                $("input:[name='phone']").val("");
				$("#staffdetail").html("<th>&nbsp;</th><td>"+_msg.msg+"</td>");
				$("#staffdetail").fadeIn();
			}
		});
	})
	$("#parentdep").change(function(){
		var curv = $(this).val();
		getSubDep(curv);
	});
}

function getSubDep(pid){
	$.post("aj_staff.php?action=getsubdep&pid="+pid,"",function(rs){
		var subdep = eval("("+rs+")");
		$("#subdep").html("");
		var op = "<option value=-1>小部门</option>";
		if(!subdep.error){
			$.each(subdep, function(i,n){
				op +="<option value="+n.id+">"+n.name+"</option>";
			});
		}
		$("#subdep").html(op);
		if(typeof(depopt_select)!="undefined"){
			$("#subdep").val(depopt_select.sid);
		}
});
}
function getParentDep(){
	$.post("aj_staff.php?action=getpardep","",function(rs){
		var pardep = eval("("+rs+")");
		var op = "<option value=-1>大部门</option>";
		if(!pardep.error){
			$.each(pardep, function(i,n){
				op +="<option value="+n.id+">"+n.name+"</option>";
			});
		}
		$("#parentdep").html("");
		$("#parentdep").html(op);
		if(typeof(depopt_select)!="undefined"){
			$("#parentdep").val(depopt_select.pid);
			getSubDep(depopt_select.pid);
		}
	});
}
function init() {

    //添加管理员提交检查
    $("#add_user").click(function(){
        var email = $("#email").val();
        var password = $("#password").val();
        var name = $("#name").val();
//        if(email == '' || password == '' || name == '') {
//            alert('前三项必添');return false;
//        }
        if(email.indexOf("@") == -1) {
            alert("请添写邮箱正确格式");return false;
        }
		if(password.length > 0 ) {
			if(password.length < 6 || password.length > 14) {
				alert('密码长度6-14位');return false;
			} else {
				//检查密码
				var pattern = /^[0-9a-zA-Z]+$/i;
				if (pattern.test(password)) {
					var stri = /[A-Za-z]/;
					var i = /[0-9]+/;
					var spec=/[,.<>{}~!@#$%^&*]/;
					
					if(!stri.test(password)){
						alert("必须包含字母");
						return false;
					}
					if(!i.test(password)){
						alert("必须包含数字");
						return false;
					}
				}
				else {
					alert("密码只能包含字母、数字");
					return false;
				}
			}
		}
        var telephone = $("#telephone").val();
        var phone = $("#phone").val();
        if(telephone == '' && phone == '') {
            alert('联系方式至少添写一项');return false;
        }
        $("#email").removeAttr("disabled");
        document.forms.upform.submit();
    });
    //禁止 or 激活管理员
    $(".loginstatus").click(function() {
        var status = $(this).attr('status');
        var staff = $(this).attr('rel');
        var txt = status == '1' ?  '确定要激活该用户吗？' : '确定要移除该用户吗？' ;
        if(confirm(txt)) {
            $.ajax({
               type: "POST",
               url: "aj_dostaff.php",
               cache: false,
               async : false,
               dataType: "json",
               data: "staff="+staff+"&status="+status,
               success: function(json){
                 if(json.errorno == 1) {
                    alert('修改成功');
                 } else {
                     alert(json.msg);
                 }
                 location.href=location.href;
               }
            });
        }
        return;
    });
    //添加通行证管理员
    $("#addmin_click").click(function() {
		$("#staffdetail").fadeOut();
		$("#staffphone").fadeOut();
		var staff  = $("input:[name='staff']").val();
		if(!$.trim(staff)){
			return false;
		}
		$.get("aj_staff.php?staff="+staff, "", function(rs){
			var _msg = eval("("+rs+")");
			if(!_msg.error){
                $("input:[name='phone']").val(_msg.phone);
                document.addadmin.submit();
			}else{
                alert('处理失败，内网不存在该用户');return false;
			}
		});
    });
    //禁止 or 激活通行证管理员
    $(".adminstatus").click(function() {
        var status = $(this).attr('status');
        var staff = $(this).attr('rel');
        var txt = status == '1' ?  '确定要激活该用户吗？' : '确定要移除该用户吗？' ;
        if(confirm(txt)) {
            $.ajax({
               type: "POST",
               url: "aj_dostaff.php",
               cache: false,
               async : false,
               dataType: "json",
               data: "staff="+staff+"&status="+status+"&type=2",
               success: function(json){
                 if(json.errorno == 1) {
                    alert('修改成功');
                 } else {
                     alert(json.msg);
                 }
                 location.href=location.href;
               }
            });
        }
        return;
    });
}
$(function(){
    list_admin_init();
    init();
});